<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp" %>
<html>
 <head>
    <title>其他入库批次管理</title>
    <!-- import css and js -->
	<%@ include file="/common/meta.jsp" %>
	<%@ include file="/common/metaInput.jsp" %>  
	<script src="${ctx}/js/ligerUI/js/plugins/ligerLayout.js" type="text/javascript"></script>
	<script type="text/javascript" src="${ctx}/js/My97DatePicker/WdatePicker.js"></script>
	<script type="text/javascript" src="${ctx}/js/jsp/yd/logic/ItemStateData.js"></script>
	<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
	<script type="text/javascript">
		var basic_Url="${ctx}/yd/logic/otherOutwh";
		var ItemStateList = ItemStateData.Rows;
		var item = eval("("+'${outwhItemBatchDO}'+")");
		var otherOutwhItemId = item.otherOutwhItemId;
		var code = item.code;
		var bd = '${itemBatchDOs}';
		var whCode= '${whCode}';
		//var delBd = '${delItemBatchDOs}';
 		//alert("bd:"+bd);
// 		function createSearch(){
// 			init(1,6,'100,100,100,100,100,100','10%');
// 			//第一行的数据
// 			addCommon(0,0,"lable","物料编码：");
// 			addCommon(0,1,"txtlike","code");
// 			addCommon(0,2,"lable","物料名称：");
// 			addCommon(0,3,"txtlike","name");
// 			addCommon(0,4,"lable","规格型号：");
// 			addCommon(0,5,"txtlike","spec");
// 		}
		
// 		function searchAP(){
// 			multipleSearch("#table1",true,"",basic_Url+"/itemlist.do?componentSequenceId="+componentSequenceId);
// 		}
		var grid;
		$(function(){
			var column1 =  [
					{ display: 'id', name: 'id',type: 'string',hide:1},
				    { display: '物料id', name: 'materialsid',width:1,hide:1},//使用selecturl时必须设置该属性
				    { display: '<font color="gray">编码</font>', name: 'code',width: 120},
				    { display: '<font color="gray">名称</font>', name: 'name',width: 120},
				    { display: '<font color="gray">型号</font>', name: 'spec' ,width: 270},
				    { display: '<font color="gray">基本单位</font>', name: 'junit' ,width: 100},
				    { display: '<font color="gray">库存数量</font>', name: 'kcsl',width: 100 },
				    { display: '<font color="gray">基本单位出库数量</font>', name: 'jquantity',width: 150},
				   	{ display: '批次<font color="red">*</font>', name: 'batch', width: 120, isSort: false,
				   		editor: {type: 'selectbatch',whcode:'whCode', databatch:'batchItem',path:'${ctx}/yd/logic/otherOutwh/getWhBatch.do',valueColumnName: 'batch', displayColumnName: 'batch',
				            ext :
				                function (rowdata)
				                {
				                    return {
				                        onBeforeOpen: function(){
				                        	getDG().dialog({
									    		title:'选择批次',
									    		width : 300,
												height : 350,
												content: 'url:' + sys_context+'/public/windowalert/itembatch.do?whCode=' + whCode+'&maCode='+rowdata.code,//
									    		lock : true,
									    		max : true,
									    		min : true,
									    		ok: function(){
									    			var rows = this.content.getSelected();
													if (rows != null) {
														grid.updateCell('batch', rows[0].batch, rowdata);
														grid.endEdit();
														getBatchkcQuantity(rowdata);
													}
									    			this.content.Close();
									        		return false;
									    	    },
									    	    cancelVal: '关闭',
									    	    cancel: function(){
									    	    	grid.endEdit();
									    	    } 
									    	}).zindex();
				                        }, render: function ()
				                        { 
				                        	var batchItem = $.parseJSON($("#batchItem").val());
				            		  		if(batchItem!=null){
				            			 	for (var i = 0; i < batchItem.length; i++)
				                     			{
				                         			if (batchItem[i]['batch'] == rowdata.batch){
				                            	 		return batchItem[i]['batch'];
				                         			} 
				                     			}
				            		  		}
				                        }
				
				                    };
				                }
							 }, render: function (item)
				            {
				   		 		var batchItem = $.parseJSON($("#batchItem").val());
						  		if(batchItem!=null){
							 	for (var i = 0; i < batchItem.length; i++)
				         			{
				             			if (batchItem[i]['batch'] == item.batch){
				                	 		return batchItem[i]['batch'];
				             			} 
				         			}
						  		}
				                return item.batch;
				            }
				    },
				    { display: '状态<font color="red">*</font>', name: 'status', width: 100,editor: { type: 'select', data: ItemStateList, valueColumnName: 'ItemStateID', displayColumnName: 'ItemStateName',onChanged:getkcQuantity}, render: function (item)
				        {
				        for (var i = 0; i < ItemStateList.length; i++)
				        {
				            if (ItemStateList[i]['ItemStateID'] == item.status){
				          	  return ItemStateList[i]['ItemStateName'];
				            }
				        }
				        return item.status;
				    }},
				    { display: '单位<font color="red">*</font>', name: 'unit', width: 100},
				    { display: '<font color="gray">单位换算率</font>', name: 'unitconvert',width:80},
					{ display: '出库数量<font color="red">*</font>', name: 'quantity',width: 150,editor : {type :'float'}, render: function (item)
				    {	
					   if(item.quantity!=''&&item.quantity!=null&&item.unitconvert!=null&&item.unitconvert!=''){
					   		grid.updateCell('jquantity', item.quantity*item.unitconvert, item);
					   }
						
				       return item.quantity;
				    }}
			                  ];
			var column2 =  [
					{ display: 'id', name: 'id',type: 'string',hide:1},
				    { display: '物料id', name: 'materialsid',width:1,hide:1},//使用selecturl时必须设置该属性
				    { display: '<font color="gray">编码</font>', name: 'code',width: 120},
				    { display: '<font color="gray">名称</font>', name: 'name',width: 120},
				    { display: '<font color="gray">型号</font>', name: 'spec' ,width: 270},
				    { display: '<font color="gray">基本单位</font>', name: 'junit' ,width: 100},
				    { display: '<font color="gray">库存数量</font>', name: 'kcsl',width: 100 },
				    { display: '<font color="gray">基本单位出库数量</font>', name: 'jquantity',width: 150},
				   	{ display: '批次<font color="red">*</font>', name: 'batch', width: 120},
				    { display: '状态<font color="red">*</font>', name: 'status', width: 100},
				    { display: '单位<font color="red">*</font>', name: 'unit', width: 100},
				    { display: '<font color="gray">单位换算率</font>', name: 'unitconvert',width:80},
					{ display: '出库数量<font color="red">*</font>', name: 'quantity',width: 150}
			        ];
			//var colunm3 = column1;                 
// 		    if('${billstatus}' != "" && '${billstatus}' != "0"){
// 		    	colunm3 = column2;    
// 		    }
 if('${billstatus}' != "add" && '${billstatus}' != "tijiao"){
	 grid = $("#table1").ligerGrid({
	        columns: column2, 
	                      enabledEdit: true,
	                      usePager: true,pageSize:1000,pageSizeOptions:[1000],enabledSort:false, checkbox: true,
	                      url : basic_Url+"/itemlist.do?otherOutwhItemId="+otherOutwhItemId+"&code="+code+"&itembatch="+bd,
	                      width: "100%", height:"98%"
	                  });
 }else{
	 grid = $("#table1").ligerGrid({
	        columns: column1, 
	                      enabledEdit: true,
	                      usePager: true,pageSize:1000,pageSizeOptions:[1000],enabledSort:false, checkbox: true,
	                      url : basic_Url+"/itemlist.do?otherOutwhItemId="+otherOutwhItemId+"&code="+code+"&itembatch="+bd,
	                      width: "100%", height:"98%",
	                      toolbar: { items: [
	                                         { text: '添加', click: insert, icon: 'add' },
	                                         { line: true },
	                                         { text: '删除', click: deleteRow, img: '${ctx}/js/ligerUI/skins/icons/delete.gif' }
	                                         ]
	                                         }
	                  });
 }
				
				otherOutwhItemId ='';
				code ='';
			$("#pageloading").hide();
		
		
		});
		function getkcQuantity(e){
	    	var rowData=grid.getRow($(e).parent()[0]);
	    	if (rowData.batch){
	    		 $.ajax({
	      		      url: "${ctx}/yd/logic/otherOutwh/getStorageQuaitity.do",
	      		      global: false,
	      		      type: "POST",
	      		      dataType: "json",
	      		      data: ({
	      		    	maCode : rowData.code,
	      				whCode : whCode,
	      				hubCode : rowData.hubCode,
	      				status : rowData.status,
	      				batch : rowData.batch
	      		      }),		     
	      		      success: function(msg){
	      		    	rowData.kcsl = msg;
	      			 	rowData.auto = true;
	      	   			grid.updateCell('kcsl',msg,rowData);
	      		      }
	      	   });
	   		}else{
	   			$.dialog.alert("请先选择批次");
	   		}
	    }
	    
	    function getBatchkcQuantity(rowData){
	    	if (rowData.batch){
	    		 $.ajax({
	      		      url: "${ctx}/yd/logic/otherOutwh/getStorageQuaitity.do",
	      		      global: false,
	      		      type: "POST",
	      		      dataType: "json",
	      		      data: ({
	      		    	maCode : rowData.code,
	      				whCode : whCode,
	      				hubCode : rowData.hubCode,
	      				status : rowData.status,
	      				batch : rowData.batch
	      		      }),		     
	      		      success: function(msg){
	      		    	rowData.kcsl = msg;
	      			 	rowData.auto = true;
	      	   			grid.updateCell('kcsl',msg,rowData);
	      		      }
	      	   });
	   		}else{
	   			$.dialog.alert("请先选择批次");
	   		}
	    }
		
		function getSelected(){
			var rows = grid.getCheckedRows();
			 return rows;
		}
		
		function Close(){
			CloseDialog();
		}
		function insert(){
			if('${billstatus}' != "add" && '${billstatus}' != "tijiao"){
				return false;
			}else{
				var manager = $("#table1").ligerGetGridManager();
				manager.addRow2({
					otherOutwhItemId:item.otherOutwhItemId,
					materialsid: item.materialsid,
		    		code: item.code,
		    		name: item.name,
		    		spec: item.spec,
		    		junit : item.junit,
		    		unit : item.unit,
		    		unitconvert : item.unitconvert,
		    		batch : ""
				});
			}
		}
		function deleteRow()
		{ if('${billstatus}' != "add" && '${billstatus}' != "tijiao"){
			return false;
		}else{
			grid.deleteSelectedRow();
		}
		}
		var ba=true;
		var num = 0;
		var jnum = 0;
		function yz(){
			var data = grid.getData();
			if (data != null && data.length > 0) {
				jnum = 0;
				num = 0;
				for ( var i = 0; i < data.length; i++) {
					if(data[i].batch==undefined ||data[i].batch.replace(/(^\s*)|(\s*$)/g, "")=="" ||data[i].batch==null){
	 					ba = false;
	 					$.dialog.alert("您第" + (i+1) + "行的批次没有填写");
	 					return false;
	 				}
	 				if(data[i].status=="" ||data[i].status==undefined ||data[i].status==null){
	 					ba = false;
	 					$.dialog.alert("您第" + (i+1) + "行的状态没有填写");
	 					return false;
	 				}
	 				for ( var j = 0; j < data.length; j++) {
	 					if(i != j){
	 						if(data[i].code==data[j].code && data[i].batch==data[j].batch && data[i].status==data[j].status){
	 							ba = false;
	 							$.dialog.alert("第"+(i+1)+"行与第"+(j+1)+"行存在相同的批次与等级!");
	 							return false;
	 						}
	 					}
	 				}
	 				jnum += parseFloat(data[i].jquantity);
	 				num  += parseFloat(data[i].quantity);
	 				if (isNaN(data[i].quantity) ||data[i].quantity==undefined ||data[i].quantity==null) {
	 					ba = false;
	 					$.dialog.alert("您第" + (i+1) + "行的数量没有填写");
	 					return false;
	 				}
	 				if (data[i].quantity <= 0) {
	 					$.dialog.alert("您第" + (i+1) + "行的入库数量不能小于等于0");
	 					ba = false;
	 					return false;
	 				}
	 				if (parseFloat(data[i].jquantity) > parseFloat(data[i].kcsl)){
			        	   $.dialog.alert("您第"+(i+1)+"行的出库数量大于库存数量");
			        	   ba=false;
			            	return false;
			        }
	 			}
			}
				ba=true;
		}
	</script>
	
  </head>
 <body style="padding:10px">  
       <div id="layout1">
       <div id="layout1">
<!--           <div class="panelBar"> -->
<!-- 	            <ul class="toolBar"> -->
<!-- 		           <li><a class="add" onclick="insert()"><span>新增</span></a></li> -->
<!-- 		       		<li><a class="delete" onclick="deleteRow()"><span>删除</span></a></li> -->
<!--                </ul> -->
<!--           </div> -->
          <div id="pageHeader" class="pageHeader">
               <div class="searchBar" id="multipleSearchDialog">
	                <table class="table" width="100%" id="searchHeader" >  
                        <tbody id="newbody">
                        </tbody>
 	                </table>
<!--                     <div class="subBar"> -->
<!-- 			             <ul> -->
<!-- 				             <li><div class="buttonActive"><div class="buttonContent"><button type="submit" id="btnSearch" onclick="insert()">添加</button></div></div></li> -->
<!--  				             <li><a class="button" onclick="clearSearch()"><span>删除</span></a></li>  -->
<!-- 			             </ul> -->
<!-- 	                </div> -->
               </div>
          </div>
      </div>
      <div position="center" id="table1">
      </div>
      <div class="clear"></div>
   </div>
    </body>
</html>